package org.me.marble;

import android.widget.*;

import android.view.View;
import android.os.Handler;

/**
 *
 * @author tungi
 */

public class GraphicsOptions implements AdapterView.OnItemClickListener,Runnable{
	private int run=-1;
    private OptionsActivity _options;
    private TwoLineArrayAdapter<String> shadow,graph;
    
    public GraphicsOptions(OptionsActivity _options){
        this._options=_options;
    }

    public void createView(){
        _options.setContentView(R.layout.graphicsoptions);

        ListView view;

        //set first litview -> first section
        shadow=new TwoLineArrayAdapter<String>(
                _options,R.layout.twolinelistitem,R.twolinelistitem.checkbox,
                R.twolinelistitem.line1,R.twolinelistitem.line2,new String[]{"Shadow"},
                new String[]{"This decrease performance"});
        view=(ListView)_options.findViewById(R.list.shadow);
        view.setAdapter(shadow);
        view.setId(R.list.shadow);
        view.setOnItemClickListener(this);
        view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        view.setItemChecked(0,OptionsLibrary.shadow);

        String[] options={"Low","Medium","High"};            

        //set second litview -> first section
        graph=new TwoLineArrayAdapter<String>(
                _options,R.layout.twolinelistitem,R.twolinelistitem.checkbox,
                R.twolinelistitem.line1,R.twolinelistitem.line2,options,
                new String[]{"Both transparency and textures are disabled",
                			 "Textures are enabled",
                			 "Both transparency and textures are enabled"});
        
        view=setListView(graph,R.list.graphicsq);
        setSelection(view,OptionsLibrary.graphics_quality);
        //set third litview -> second section
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(_options,
        		android.R.layout.simple_list_item_multiple_choice,options);
        
        view=setListView(adapter,R.list.objectq);
        setSelection(view,OptionsLibrary.objects_quality);
        //set fourth litview -> third section
        view=setListView(adapter,R.list.textureq);
        setSelection(view,OptionsLibrary.texture_quality);
        
        (new Handler()).postDelayed(this,100);
    }

    public ListView setListView(ArrayAdapter<String> adapter,int id){
        ListView view;
        view=(ListView)_options.findViewById(id);
        view.setAdapter(adapter);
        view.setId(id);
        view.setOnItemClickListener(this);
        view.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        return view;
    }

    public void setSelection(ListView view,String ref){
        if(ref.equals("low"))
            view.setItemChecked(0,true);
        else if(ref.equals("medium"))
            view.setItemChecked(1,true);
        else if(ref.equals("high"))
            view.setItemChecked(2,true);
    }

    public String getOptions(int pos){
        switch(pos){
            case 0: return "low";
            case 1: return "medium";
            case 2: return "high";
            default: break;
        }
        return "";
    }
    
    public int getOptionsPos(String option){
        if(option.equals("low"))
        	return 0;
        else if(option.equals("medium"))
        	return 1;
        else if(option.equals("high"))
        	return 2;
        else 
        	return -1;
    }

    public void onItemClick(AdapterView<?> arg0,View view,int pos,long id){
    	//TODO find a better way to toogle checkbox
        if(arg0.getId()==R.list.shadow){
            OptionsLibrary.shadow=!OptionsLibrary.shadow;
            view.setSelected(OptionsLibrary.shadow);
            
            run=0;
            (new Handler()).postDelayed(this,50);
        }
        else if(arg0.getId()==R.list.graphicsq){
            view.setSelected(true);            
        	graph.setChecked(getOptionsPos(OptionsLibrary.graphics_quality),false); 
            OptionsLibrary.graphics_quality=getOptions(pos);
            
            run=1;
            (new Handler()).postDelayed(this,50);
        }
        else if(arg0.getId()==R.list.objectq){
            view.setSelected(true);
            OptionsLibrary.objects_quality=getOptions(pos);
        }
        else if(arg0.getId()==R.list.textureq){
            view.setSelected(true);
            OptionsLibrary.texture_quality=getOptions(pos);
        }
    }

    
	@Override
	public void run(){
		if(run==-1 || run==0)
			shadow.setChecked(0,OptionsLibrary.shadow);
		if(run==-1 || run==1)
			graph.setChecked(getOptionsPos(OptionsLibrary.graphics_quality),true);
	}
}
